//
//  DWBaseResponse.swift
//  DWSwift
//
//  Created by GDL on 2025/6/23.
//

import Foundation

struct DWBaseResponse<T: Decodable>: Decodable {
    let code: Int              // 状态码
    let data: T?               // 泛型数据载体（核心设计）
    let msg: String?           // 可选提示信息
    
    enum CodingKeys: String, CodingKey {
        case data = "data"
        case msg = "msg"
        case code = "code"
        
    }
    
    // 快速校验成功状态
    var isSuccess: Bool { // 根据服务定义 0 是业务正常，其它都不正常
        return code == 0
    }
    
    
    // 若失败，抛出业务错误
    func getMsg() throws -> String {
        guard isSuccess else {
            throw DWNetworkError.businessError(code: code, message: msg ??  "Unknown error")
        }
        return msg ?? "Unknown error"
    }
}
